package NWPackage;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.text.html.HTMLDocument.Iterator;

import Readers.InputsReader;

public class PairPreferdCandidateMatrix extends Matrix {

	// Map <Agent,List<Queries> 
	private Map<String,List<PairPreferdCandidateCell>> allAgentsCells;
	
	public PairPreferdCandidateMatrix() {
		allAgentsCells = new HashMap<String, List<PairPreferdCandidateCell>>();
	}

	public PairPreferdCandidateMatrix(Cell[] cells) {
		super(cells);
		allAgentsCells = new HashMap<String, List<PairPreferdCandidateCell>>();
	}
	
	
	public void initial()
	{
		this.setCells(initialPairsPermutations());
	}

	private Cell[] initialPairsPermutations() {	
		PairPreferdCandidateCell[] Cells = new PairPreferdCandidateCell[Calculations.calcNumberOfQueries()];
		int index = 0;
		int numCandidates = InputsReader.getNumCandidates();
		String[] candidates = InputsReader.getCandidates();
		String[] agents = InputsReader.getAgents();
		
		for (String agent : agents) {
			int agentCellsIndex = 0;
			PairPreferdCandidateCell[] agentCells = new PairPreferdCandidateCell[Cells.length/agents.length];
			for (int i = 0; i < numCandidates; i++) {
				for (int j = i+1; j < numCandidates; j++) {
					Cells[index] = new PairPreferdCandidateCell(agent,index, candidates[i], candidates[j]);
					agentCells[agentCellsIndex] = Cells[index];
					index++;
					agentCellsIndex++;
				}
			}
			allAgentsCells.put(agent, Arrays.asList(agentCells));
		}
		return Cells;
	}
	
	public PairPreferdCandidateCell getCell(int index)
	{
		if (index < cells.length)
			return (PairPreferdCandidateCell)cells[index];
		return null;
	}
	
	public PairPreferdCandidateCell getCell(PairPreferdCandidateCell cell)
	{
		String agentName = cell.getAgentName();
		List<PairPreferdCandidateCell> newAgentCells = this.allAgentsCells.get(agentName);
		if (newAgentCells!=null){
			for (PairPreferdCandidateCell existCell : newAgentCells) {
				if (existCell.equals(cell))
					return existCell;
			}
		}
		return null;
	}

	public void setVote(int index, String agentPreferred) {
		try{
			this.getCell(index).setPrefered_C(agentPreferred);
		}
		catch (Exception e) {
			System.out.println("cannot find Cell number: " + index);
			throw new NullPointerException();
		}
	}

	public Map<String,List<PairPreferdCandidateCell>> getListedCells() {
		return this.allAgentsCells;
	}

	public void setAnswer(PairPreferdCandidateCell query) {
		String agentName = query.getAgentName();
		List<PairPreferdCandidateCell> agentCells = this.getCellsByAgent(agentName);
		if (agentCells == null){
			List<PairPreferdCandidateCell> newList= new ArrayList<PairPreferdCandidateCell>();
			newList.add(query);
			this.allAgentsCells.put(agentName,newList);
		}
		else
		{
			agentCells.add(query);
			this.allAgentsCells.put(agentName,agentCells);
		}
		
			
		
	}
	public List<PairPreferdCandidateCell> getCellsByAgent(String agentName)
	{
		List<PairPreferdCandidateCell> agentCellsToReturn = null;
		
		List<PairPreferdCandidateCell> agentCells = this.allAgentsCells.get(agentName);
		if (agentCells!= null)
		{
			agentCellsToReturn = new ArrayList<PairPreferdCandidateCell>();
			agentCellsToReturn.addAll(agentCells);
		}
		return agentCellsToReturn;
	}

	public void setAnswers(List<PairPreferdCandidateCell> queries) {
		for (PairPreferdCandidateCell answer : queries) {
			this.setAnswer(answer);
		}
		
	}

	public void setVote(PairPreferdCandidateCell query) {
		this.getCell(query).setPrefered_C(query.getPrefered_C());
		
		
	}
	
	/*Lihi's addition*/
/*	
	public Map<String,List<PairPreferdCandidateCell>> relevetTrios(String itemA, String itemB,List<String> optionalWinners){
		Map<String,List<PairPreferdCandidateCell>> trios = new HashMap<String, List<PairPreferdCandidateCell>>();
        String[] allAgents = InputsReader.getAgents();		
		for (String agent : allAgents) {		
			List<PairPreferdCandidateCell> itemsForAgent = this.allAgentsCells.get(agent);		
			PairPreferdCandidateCell[] updatedItemsforAgent = new PairPreferdCandidateCell[Cells.length/agents.length];

			java.util.Iterator<PairPreferdCandidateCell> it = itemsForAgent.iterator();
			while (it.hasNext()){
				PairPreferdCandidateCell cellQ = it.next();
			//	String voterName = cellQ.getAgentName();						
				String [] cands = cellQ.getCandidates();
				if (cellQ.isExistIn(optionalWinners)) //if the chosen exist in optionalwinners  
					if( (cands[0].equals(itemA))||(cands[1].equals(itemA)))  //if one of the chosen item is a MAX
						if( (cands[0].equals(itemB))||(cands[1].equals(itemB))  ) //if one of the chosen is the 2nd MAX
							updatedItemsforAgent.add(cellQ);				
			}//while
			trios.put(agent, updatedItemsforAgent);											
		}
			
		return trios;
	}
*/
	
}
